import sys
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QCheckBox, QMessageBox


class MainWindow(QWidget):#继承QWidget
    def __init__(self, *args, **kwargs):#self is default parameter;you can add other parameters;
        #*args is mean any parameters; *kwargs is mean any dictronary parameters;
        super().__init__()#继承执行父类的构造函数
        self.setWindowTitle("checkbox控件") #set window title;be overrides the title
        ###############################################
        self.resize(400, 300)#设置窗口大小
        layout = QVBoxLayout()
        self.button=QPushButton("register")#创建一个按钮
        self.checkbox=QCheckBox("ok")#创建一个复选框 yes or no
        self.button.clicked.connect(self.register)

        layout.addWidget(self.button)
        layout.addWidget(self.checkbox)
        self.setLayout(layout)#把布局添加到窗口中

        bg_color = kwargs.get('setbackgroundColor', None)
        if bg_color:
            self.setStyleSheet(f"background-color: {bg_color};")
        self.show()
        ###############################################
    def register(self):
        if self.checkbox.isChecked():#True or False 是否勾选
            print("register")
            QMessageBox.information(self, "success", "register success")
        else:
            print("cancel")
            QMessageBox.critical(self, "fail", "must checked")

if __name__ == "__main__":
    #print(sys.argv) #['D:\\u\\test\\gitee\\pythonqt\\test1\\改造程序\\new_hello2.py'] print sys.argv;default is this file path.
    app=QApplication(sys.argv)#get parameters from command line.
    w=MainWindow(setbackgroundColor="lightblue") #use MainWindow created a window.
    w.setWindowTitle("checkbox控件")
    w.show()#显示窗口
    #app.exec()#运行程序

    sys.exit(app.exec()) #app.exec() is first stop,then out process

    #import time #test app end,but process is not stop.
    #for i in range(10):#genarate a dictionary
    #    print(i)
    #    time.sleep(1)
# app=QApplication([])
# window=QWidget(windowTitle="Hello World",)#创建一个窗口
# window.show()#显示窗口
# app.exec()#运行程序
